package com.qsw.refreshrecyclerview.test;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.qsw.refreshrecyclerview.R;
import com.qsw.refreshrecyclerview.RefreshRecyclerView.listener.OnItemClickListener;
import com.qsw.refreshrecyclerview.RefreshRecyclerView.view.ScrollTableView;
import com.qsw.refreshrecyclerview.RefreshRecyclerView.adapter.ScrollTableViewAdapter;

import java.util.ArrayList;
import java.util.List;

public class RRVTestScrollActivity extends Activity {
    private static final String TAG = "TAG_RRVTestScrollActivity";
    private ScrollTableView mScrollTableView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.rrv_activity_test_scroll);

        initView();
    }

    private void initView() {
        mScrollTableView = findViewById(R.id.rrvl_rrv);

        List<TestBean> dataList = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            TestBean testBean = new TestBean("姓名" + i, "数据1" + i, "数据2" + i, "数据3" + i, "数据4" + i);
            dataList.add(testBean);
        }
        mScrollTableViewAdapter.setData(dataList);
        mScrollTableView.setAdapter(mScrollTableViewAdapter);
        mScrollTableView.notifuDataSetChanged();

        mScrollTableView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onClick(View view, int position) {
                Log.d(TAG, "onclick - " + System.currentTimeMillis() + " : " + position);
            }

            @Override
            public void onLongClick(View view, int position) {
                super.onLongClick(view, position);
                Log.d(TAG, "onLongClick - " + System.currentTimeMillis() + " : " + position);
            }
        });

        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<TestBean> dataList = new ArrayList<>();
                for (int i = 0; i < 3; i++) {
                    TestBean testBean = new TestBean("新增姓名" + i, "新增数据1" + i, "新增数据2" + i, "新增数据3" + i, "新增数据4" + i);
                    dataList.add(testBean);
                }
                mScrollTableViewAdapter.addData(dataList);
                mScrollTableView.notifuDataSetChanged();
            }
        });
    }

    private ScrollTableViewAdapter<TestBean> mScrollTableViewAdapter = new ScrollTableViewAdapter<TestBean>(null) {
        @Override
        public View onGetLeftTopLayout() {
            return LayoutInflater.from(RRVTestScrollActivity.this).inflate(R.layout.rrv_test_scroll_left_top, null, false);
        }

        @Override
        public View onGetRightTopLayout() {
            return LayoutInflater.from(RRVTestScrollActivity.this).inflate(R.layout.rrv_test_scroll_right_top, null, false);
        }

        @Override
        public float onGetLeftWidthPercent() {
            return 0.3F;
        }

        @Override
        public RecyclerView.ViewHolder onCreateLeftBottomViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new LeftBottomViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.rrv_test_scroll_left_bottom_item, parent, false));
        }

        @Override
        public void onBindLeftBottomViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            ((LeftBottomViewHolder) holder).tvName.setText(getItemBean(position).getName());
        }

        @Override
        public RecyclerView.ViewHolder onCreateRightBottomViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new RightBottomViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.rrv_test_scroll_right_bottom_item, parent, false));
        }

        @Override
        public void onBindRightBottomViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            ((RightBottomViewHolder) holder).tvData1.setText(getItemBean(position).getData1());
            ((RightBottomViewHolder) holder).tvData2.setText(getItemBean(position).getData2());
            ((RightBottomViewHolder) holder).tvData3.setText(getItemBean(position).getData3());
            ((RightBottomViewHolder) holder).tvData4.setText(getItemBean(position).getData4());
        }

        @Override
        public int getItemCount() {
            return mData == null ? 0 : mData.size();
        }

        class LeftBottomViewHolder extends RecyclerView.ViewHolder {
            TextView tvName;

            public LeftBottomViewHolder(View itemView) {
                super(itemView);
                tvName = itemView.findViewById(R.id.tv_scroll_left_bottom_name);
            }
        }

        class RightBottomViewHolder extends RecyclerView.ViewHolder {
            TextView tvData1;
            TextView tvData2;
            TextView tvData3;
            TextView tvData4;

            public RightBottomViewHolder(View itemView) {
                super(itemView);
                tvData1 = itemView.findViewById(R.id.tv_scroll_right_bottom_data1);
                tvData2 = itemView.findViewById(R.id.tv_scroll_right_bottom_data2);
                tvData3 = itemView.findViewById(R.id.tv_scroll_right_bottom_data3);
                tvData4 = itemView.findViewById(R.id.tv_scroll_right_bottom_data4);
            }
        }
    };

    class TestBean {
        String name;
        String data1;
        String data2;
        String data3;
        String data4;

        public TestBean(String name, String data1, String data2, String data3, String data4) {
            this.name = name;
            this.data1 = data1;
            this.data2 = data2;
            this.data3 = data3;
            this.data4 = data4;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getData1() {
            return data1;
        }

        public void setData1(String data1) {
            this.data1 = data1;
        }

        public String getData2() {
            return data2;
        }

        public void setData2(String data2) {
            this.data2 = data2;
        }

        public String getData3() {
            return data3;
        }

        public void setData3(String data3) {
            this.data3 = data3;
        }

        public String getData4() {
            return data4;
        }

        public void setData4(String data4) {
            this.data4 = data4;
        }
    }
}
